<?php

namespace Modules\Admin\Controllers\Ucenter;

use Modules\Wechat\Model\Data\Message\Exchange as MsgExchange;
use Modules\Wechat\Model\Data\OfficialAccount;
use Modules\Wechat\Model\Data\OfficialAccount\Handling;
use Modules\Wechat\Model\Data\OfficialAccount\Menu as OaMenuData;
use Modules\Wechat\Model\Service\OfficialAccount\Menu as OaMenuService;
use S\Request;

/**
 * @name 微信公众号管理
 * @package Modules\Admin\Controllers
 */
class Wechat extends Controller
{

    const DISPLAY_VIEW_SYS = true;
    const DISPLAY_VIEW_APP = false;

    /** @var bool 是否显示phplib中是视图:通过$this->setDisplayView()设置 */
    protected $sys_view = self::DISPLAY_VIEW_APP;

    /**
     * 设置显示哪里的视图文件
     *
     * @param bool $display_view 为self::DISPLAY_VIEW_SYS时显示phplib2中的视图
     *                           为self::DISPLAY_VIEW_APP时显示上层应用中的视图
     *
     * @return $this
     */
    protected function setDisplayView($display_view = self::DISPLAY_VIEW_APP)
    {
        $this->sys_view = boolval($display_view);
        return $this;
    }

    /**
     * 注册模板变量
     *
     * @param string $name
     * @param mixed  $value
     *
     * @return $this
     */
    protected function assign($name, $value)
    {
        $this->response[$name] = $value;
        return $this;
    }

    /**
     * @name 获取正在处理的公众号的微信号
     *
     * @return string
     */
    protected function getOaHandling()
    {
        return Request::get('oa_handling', Handling::get());
    }

    /**
     * @name         设置正在处理的公众号的微信号
     *
     * @param string $oa_handling
     *
     * @return string
     * @throws \Base\Exception\Data
     */
    protected function setOaHandling($oa_handling)
    {
        return Handling::set($oa_handling);
    }

    /**
     * @name 欢迎页
     */
    public function indexAction()
    {
        $this->setDisplayView(static::DISPLAY_VIEW_SYS);
        $this->response['page_title']  = '微信-欢迎页';
        $this->response['page_header'] = '欢迎使用微信管理模块';
    }

    /**
     * @name 公众号 - 列表
     */
    public function oaListAction()
    {
        $this->setDisplayView(static::DISPLAY_VIEW_SYS);
        $OAccount         = new OfficialAccount;
        $handling_account = Handling::get();

        $this->assign('page_title', '微信-公众号列表')
             ->assign('page_header', '所有公众号')
             ->assign('handling', $handling_account)
             ->assign('handling_detail', $OAccount->getDetail($handling_account))
             ->assign('list', $OAccount->getAll($handling_account, true));
    }

    /**
     * @name 公众号 - 设置正在操作的公众号的微信号
     */
    public function oaSetHandlingAction()
    {
        $this->setDisplayView(static::DISPLAY_VIEW_SYS);
        $this->assign('account', $this->setOaHandling(Request::post('account', '')));
    }

    /**
     * @name 公众号 - 获取正在操作的公众号的微信号
     */
    public function oaGetHandlingAction()
    {
        $this->setDisplayView(static::DISPLAY_VIEW_SYS);
        $this->assign('account', $this->getOaHandling());
    }

    /**
     * @name 菜单 - 本地菜单列表
     */
    public function menuListAction()
    {
        $Menu             = new OaMenuData;
        $handling_account = Request::get('oa_handling', Handling::get());
        $this->setDisplayView(static::DISPLAY_VIEW_SYS);
        $this->assign('page_title', '微信-菜单-自定义菜单')
             ->assign('page_header', '自定义菜单')
             ->assign('type_list', $Menu->getTypeList())
             ->assign('menu', $Menu->getByAccount($handling_account))
             ->assign('top_list', $Menu->getTopByAccount($handling_account))
             ->assign('status_list', $Menu::getStatusList())
             ->assign('oa_list', (new OfficialAccount)->getAll($handling_account))
             ->assign('oa_handling', $handling_account);
    }

    /**
     * @name 菜单 - 创建本地菜单
     */
    public function menuCreateAction()
    {
        $account   = Request::post('oa', Handling::get());
        $parent_id = Request::post('parent_id');

        $created = $parent_id
            ? (new OaMenuData())->addSub(
                $account,
                $parent_id,
                Request::post('type'),
                Request::post('name'),
                Request::post('key'),
                Request::post('url'),
                Request::post('media_id'),
                Request::post('sequence'),
                Request::post('status')
            )
            : (new OaMenuData())->add(
                $account,
                Request::post('type'),
                Request::post('name'),
                Request::post('key'),
                Request::post('url'),
                Request::post('media_id'),
                Request::post('sequence'),
                Request::post('status')
            );
        $this->assign('created', $created);
    }

    /**
     * @name 菜单 - 更新本地菜单
     */
    public function menuUpdateAction()
    {
        $updated = (new OaMenuData())->saveById(
            Request::post('id'),
            Request::post('oa', Handling::get()),
            Request::post('type', null),
            Request::post('name', null),
            Request::post('key', null),
            Request::post('url', null),
            Request::post('media_id', null),
            Request::post('parent_id', null),
            Request::post('sequence', null),
            Request::post('status', null)
        );
        $this->assign('created', $updated);
    }

    /**
     * @name 菜单 -删除本地菜单
     */
    public function menuDeleteAction()
    {
    }

    /**
     * @name 菜单 - 回收本地菜单
     */
    public function menuRecoverAction()
    {
    }

    /**
     * @name 菜单 - 发布至公众号
     *
     * @throws \Base\Exception\Service
     */
    public function menuReleaseAction()
    {
        $released = (new OaMenuService)->release(Request::post('oa', Handling::get()));
        $this->assign('released', $released);
    }

    /**
     * @name 消息交互记录 - 查询
     */
    public function meListAction()
    {
        $oa          = Request::get('oa', Handling::get());
        $type        = Request::get('type', '');
        $from        = Request::get('from', '');
        $to          = Request::get('to', '');
        $stime_start = Request::get('stime-start', '') ?: '';
        $stime_end   = Request::get('stime-end', '') ?: '';

        $this->setDisplayView(static::DISPLAY_VIEW_SYS);
        $this->assign('page_title', '微信-消息交互记录-查询')
             ->assign('page_header', '查询')
             ->assign('oa_list', (new OfficialAccount)->getAll($oa))
             ->assign('type_list', MsgExchange::getMsgTypeList($type))
             ->assign('oa', $oa)
             ->assign('type', $type)
             ->assign('from', $from)
             ->assign('to', $to)
             ->assign('stime_start', $stime_start)
             ->assign('stime_end', $stime_end)
             ->assign(
                 'list',
                 (new MsgExchange)->getList($oa, $type, $from, $to, $stime_start, $stime_end)
             );
    }

    public function daoCreateAction()
    {
        echo '<pre>';
        \Modules\Wechat\Model\Util\Dao\Creator::echoDaoFile([\S\Request::get('t')]);
        exit;
    }


}